package com.chunxuan.langquan.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.chunxuan.langquan.MainActivity;
import com.chunxuan.langquan.R;
import com.chunxuan.langquan.base.BaseResult2;
import com.chunxuan.langquan.base.Config;
import com.chunxuan.langquan.base.Global;
import com.chunxuan.langquan.dao.bean.GradeInfo;
import com.chunxuan.langquan.dao.bean.IsShowBean;
import com.chunxuan.langquan.dao.http.APIRetrofit;
import com.chunxuan.langquan.support.util.Logg;
import com.chunxuan.langquan.ui.adapter.ExtendedViewPager;
import com.chunxuan.langquan.ui.adapter.FmPagerAdapter;
import com.chunxuan.langquan.ui.fragment.GuidePagerFragment;
import com.ruitu.arad.Arad;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * 启动页加载视频
 */
public class GuidePagerActivity extends FragmentActivity {

//    @Bind(R.id.vp_guide)
    ExtendedViewPager vpGuide;
//    @Bind(R.id.tv_enter)
    TextView tvEnter;
//    @Bind(R.id.ll_dot)
    LinearLayout llDot;
    private FmPagerAdapter pagerAdapter;
    private List<Fragment> fragments = new ArrayList<>();
    private int videoRes = R.raw.guide1;
    private int is_show=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_guidepager);
        vpGuide=findViewById(R.id.vp_guide);
        tvEnter=findViewById(R.id.tv_enter);
        llDot=findViewById(R.id.ll_dot);
//        ButterKnife.bind(this);
        init();
        isShow();
    }
    private void isShow() {
        APIRetrofit.getDefault().isShow("")
                .compose(RxUtil.<BaseResult2<IsShowBean>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<IsShowBean>>() {
                    @Override
                    public void accept(BaseResult2<IsShowBean> result2) throws Exception {
                        if (result2.isSuccess()) {
                            Logg.e("==是否展示=="+result2.getData().getIs_show());
                            is_show=result2.getData().getIs_show();
//                            is_show=1;
                            Global.IS_SHOW=is_show;
                            Arad.preferences.putInteger(Config.IS_SHOW,is_show).flush();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                        Global.IS_SHOW=1;
                        Arad.preferences.putInteger(Config.IS_SHOW,1).flush();
                    }
                });
    }
    private void init() {
        vpGuide.setOffscreenPageLimit(1);
            GuidePagerFragment fragment = new GuidePagerFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("res", videoRes);
            bundle.putInt("page", 0);
            fragment.setArguments(bundle);
            fragments.add(fragment);
        pagerAdapter = new FmPagerAdapter(fragments, getSupportFragmentManager());
        vpGuide.setAdapter(pagerAdapter);

        tvEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入主页面
                String mobile = Arad.preferences.getString(Config.USER_PREF_KEY);
                String pwd = Arad.preferences.getString(Config.PASSWORD_PREF_KEY);
                if(!TextUtils.isEmpty(mobile)&&!TextUtils.isEmpty(pwd)){
                    if(is_show==1){
                        Intent intent=new Intent(GuidePagerActivity.this, GuidActivity.class);
                        startActivity(intent);
                        finish();
                    }else if(is_show==0){
                        Intent intent=new Intent(GuidePagerActivity.this, MainActivity.class);
                        intent.putExtra("guid_id",0);
                        startActivity(intent);
                        finish();
                    }
                }else{
                    Intent intent=new Intent(GuidePagerActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });

    }


    public void next(int positon) {
        int i = vpGuide.getCurrentItem();
        if (positon == i) {
            positon += 1;
            vpGuide.setCurrentItem(positon, true);
        }
    }
}
